

// pages/blog/blog.js
let keywowrd = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modelShow: false,
    blogList: []
  },

  onPublish() {
    let that = this;
    wx.getSetting({
      success(res) {
        console.log(res);
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(res) {
              console.log(res);
              that.loginSuccess({
                detail: res.userInfo
              });
            }
          })
        } else {
          that.setData({
            modelShow: true
          })
        }
      }
    })


  },
  loginFail() {
    wx.showModal({
      title: "授权用户才能发布",
      content: ""
    })
  },
  loginSuccess(event) {
    const detail = event.detail;
    wx.navigateTo({
      url: `../blog-edit/blog-edit?nickName=${detail.nickName}&avatarUrl=${detail.avatarUrl}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadBlogList();
  },

  _loadBlogList(start = 0) {
    wx.showLoading({
      title: '加载中...',
    })
    wx.cloud.callFunction({
      name: 'blog',
      data: {
        start,
        keywowrd,
        count: 10,
        $url: 'list'
        
      }
    }).then(res => {
      wx.hideLoading();
      this.setData({
        blogList: this.data.blogList.concat(res.result)
      })
      console.log(this.data.blogList, "blogList")
    })
  },

  goComment(event) {
    wx.navigateTo({
      url: '../../pages/blog-comment/blog-comment?blogId='+ event.target.dataset.blogid,
    })
  },

  onSearch(event) {
    console.log(event.detail.keyWords);
    keywowrd = event.detail.keyWords;
    this.setData({
      blogList: []
    })
    this._loadBlogList(0);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      blogList: []
    })
    this._loadBlogList(0);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this._loadBlogList(this.data.blogList.length);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (event) {
    console.log(event,"event")
    let blogObj = event.target.dataset.blog;
    return {
      title: blogObj.content,
      path: '/pages/blog-comment/blog-comment?blogId='+blogId._id,
      // imageUrl: ''
    }
  }
})